use crate::dbs::sqlite::Peer;
use async_graphql::{Context, ID};
use derivative::Derivative;
use rbatis::rbatis::Rbatis;
use serde::{Deserialize, Serialize};
use serde_json::Result;

use crate::features::gfilter::{
  self, DateTimeFilterIn, IntFilterIn, StringExactFilterIn, StringPatternFilterIn,
};
use crate::features::gnode::KV;
use crate::features::quizzes::services;
use crate::features::sgraph::modelsdbo::{EdgeDBO, NodeDBO};

#[derive(async_graphql::SimpleObject, Serialize, Deserialize, Clone)]
#[graphql(complex)]
#[derive(Derivative)]
#[derivative(Default)]
/// this is Nodetity ,i.e. NodeEntity
pub struct Nodetity {
  /// the node id
  pub id: ID,
  pub body: String,
  pub created_at: Option<i64>,
  pub updated_at: Option<i64>,
}

#[derive(async_graphql::SimpleObject, Serialize, Deserialize, Clone, Derivative)]
#[derivative(Default)]
/// this is Nodedge, i.e. NodeEdge
pub struct Nodedge {
  /// subject id , should reference to Nodeity.id
  pub subject: ID,
  /// the relationship of subject to object, e.g. (user_abc) is (tester) of (quiz_xyz)
  pub predicate: String,
  /// object id, should reference to Nodeity.id  
  pub object: ID,
  /// the edge properties
  pub properties: String,
  pub created_at: Option<i64>,
  pub updated_at: Option<i64>,
}

#[async_graphql::ComplexObject]
impl Nodetity {
  pub async fn from(&self, ctx: &Context<'_>) -> String {
    let mut from = String::new();
    from.push_str(&self.id);
    from.push_str("<");
    from.push_str("nodedbo");
    from.push_str(">");

    from
  }
}

impl From<NodeDBO> for Nodetity {
  fn from(item: NodeDBO) -> Self {
    Nodetity {
      id: ID::from(item.id),
      body: item.body,
      created_at: item.created_at,
      updated_at: item.updated_at,
    }
  }
}

impl From<EdgeDBO> for Nodedge {
  fn from(item: EdgeDBO) -> Self {
    Nodedge {
      subject: ID::from(item.subject),
      predicate: item.predicate,
      object: ID::from(item.object),
      properties: item.facet,
      created_at: item.created_at,
      updated_at: item.updated_at,
    }
  }
}
